package com.szxy;

import com.szxy.service.StorageService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: zwz
 * @Date:2020/7/15 17:28
 * @Description:com.szxy
 * @Version:1.0
 **/
@SpringBootApplication
@EnableDiscoveryClient
@MapperScan("com.szxy.mapper")
@RestController
public class StorageServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(StorageServiceApplication.class, args);
    }

    @Autowired
    private StorageService storageService;

    /**
     * 扣库存
     * @param productName
     * @param count
     * @return
     */
    @GetMapping("/dePuct/{productName}/{count}")
    public ResponseEntity<Void> reduceInventory(
            @PathVariable String productName,
            @PathVariable Integer count) {

        if(null == productName || "".equals(productName)) {
            throw new IllegalArgumentException(String.format("%s 参数不合法", productName));
        }
        if(count < 0) {
            throw new IllegalArgumentException(String.format("%s 不用小于零", count));
        }
        // 扣库存
        this.storageService.reduceInventory(productName, count);
        // 响应结果
        return ResponseEntity.ok().build();
    }

}
